package com.cqc.manage.service.Imp.business;


import cn.hutool.core.date.DateUtil;
import com.cqc.manage.entity.CostStatistics;
import com.cqc.manage.entity.EngineerData;
import com.cqc.manage.repositories.CostStatisticsRepositories;
import com.cqc.manage.repositories.EngineerDataRepositories;
import com.cqc.manage.util.BaseValue;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EngineerDataService {

    @Autowired
    private EngineerDataRepositories engineerDataRepositories;

    @Autowired
    private BusinessEchartService businessEchartService;

    @Autowired
    private CostStatisticsRepositories costStatisticsRepositories;

    public void saveEngineerData(){
//        init();
        for (int i = 1 ;i<= DateUtil.month(new Date())+1 ; i++){
            for (String engName: BaseValue.getEngName()){
                for (String natureName:BaseValue.getNature()){
                    List<CostStatistics> byEngeerAndAndNature = costStatisticsRepositories.findByEngeerAndAndNature(engName, natureName);
                    EngineerData byEngineerName = engineerDataRepositories.findByEngineerNameAndNature(engName,natureName);
                    Float sum = 0.0f;
                    for (CostStatistics costStatistics : byEngeerAndAndNature) {
                        int year = DateUtil.year(new Date());
                        if (DateUtil.year(DateUtil.parse(costStatistics.getStartDate())) == year && (DateUtil.month(DateUtil.parse(costStatistics.getStartDate()))+1) == i) {
                            if (!costStatistics.getAgreementAmount().equals("")) {
                                sum += Float.valueOf(costStatistics.getAgreementAmount());
                            }
                        }
                    }
                    if (byEngineerName != null){
                        setData(i, byEngineerName, sum);
                        engineerDataRepositories.save(byEngineerName);
                    }else {
                        EngineerData engineerData = new EngineerData();
                        engineerData.setEngineerName(engName);
                        engineerData.setNature(natureName);
                        setData(i,engineerData,sum);
                        engineerData.setId(UUID.randomUUID().toString().replace("-",""));
                        engineerDataRepositories.save(engineerData);
                    }
                }
            }
        }
    }

    public void setData(int i,EngineerData engineerData,Float data){
        switch (i){
            case 1:
                engineerData.setOneMonth(data.toString());break;
            case 2:
                engineerData.setTwoMonth(data.toString());break;
            case 3:
                engineerData.setThreeMonth(data.toString());break;
            case 4:
                engineerData.setFourMonth(data.toString());break;
            case 5:
                engineerData.setFiveMonth(data.toString());break;
            case 6:
                engineerData.setSixMonth(data.toString());break;
            case 7:
                engineerData.setSevenMonth(data.toString());break;
            case 8:
                engineerData.setEigthMonth(data.toString());break;
            case 9:
                engineerData.setNineMonth(data.toString());break;
            case 10:
                engineerData.setTenMonth(data.toString());break;
            case 11:
                engineerData.setElevenMonth(data.toString());break;
            case 12:
                engineerData.setTwelveMonth(data.toString());break;
        }
    }


    public void init(){
        for (String engName: BaseValue.getEngName()) {
            for (String natureName : BaseValue.getNature()) {
                EngineerData engineerData = new EngineerData();
                engineerData.setEngineerName(engName);
                engineerData.setNature(natureName);
                engineerData.setId(UUID.randomUUID().toString().replace("-",""));
                engineerDataRepositories.save(engineerData);
            }
        }
    }


    public Map<String,Map<Integer,List<Float>>> getEngineerData(){
        Map<String,Map<Integer,List<Float>>> map_all = new HashMap<>();
        for (String nature:BaseValue.getNature()){
            Map<Integer,List<Float>> map = new HashMap<>();
            for (int i = 1; i <= DateUtil.month(new Date())+1 ; i++){
                List<Float> list = new ArrayList<>();
                for (String engName:BaseValue.getEngName()){
                    EngineerData byEngineerNameAndNature = engineerDataRepositories.findByEngineerNameAndNature(engName, nature);
                    list.add(Float.valueOf(getData(i, byEngineerNameAndNature)));
                }
                map.put(i,list);
            }
            map_all.put(nature,map);
        }

        return map_all;
    }


    public String getData(int i,EngineerData engineerData){
        String sum = null;
        switch (i){
            case 1:
                sum = engineerData.getOneMonth();break;
            case 2:
                sum = engineerData.getTwoMonth();break;
            case 3:
                sum = engineerData.getThreeMonth();break;
            case 4:
                sum = engineerData.getFourMonth();break;
            case 5:
                sum = engineerData.getFiveMonth();break;
            case 6:
                sum = engineerData.getSixMonth();break;
            case 7:
                sum = engineerData.getSevenMonth();break;
            case 8:
                sum = engineerData.getEigthMonth();break;
            case 9:
                sum = engineerData.getNineMonth();break;
            case 10:
                sum = engineerData.getTenMonth();break;
            case 11:
                sum = engineerData.getElevenMonth();break;
            case 12:
                sum = engineerData.getTwelveMonth();break;
        }

        return sum;
    }




}
